package client.GUI.dashboard;

import java.awt.BorderLayout;
import java.awt.event.*;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.LinkedList;

import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.*;
import javax.swing.JPanel;
import java.awt.Color;

import client.GUI.GUIConstants;
import client.backend.order.Order;
import client.backend.user.UserInformation;

/**
 * Generates a table of data for all the closed orders.
 *
 */
public class OrderHistoryPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	JTable table;
	private static OrderHistoryPanel instance = null;
	
	/**
	 * Default Constructor. Generate the table.
	 */
	private OrderHistoryPanel() {
		JLabel title = new JLabel("Order History");
		
		
		title.setBounds(0, 345, 512, 30);
    	title.setForeground(GUIConstants.Text_Color);
    	title.setHorizontalAlignment(SwingConstants.CENTER);

		this.add(title);
		createTable();
		        
		JScrollPane scrollPane = new JScrollPane(table);
		        
		this.add(scrollPane, BorderLayout.CENTER);
		this.setBackground(Color.black);
	}
	
	/**
	 * Get data from an instance
	 */
	public static OrderHistoryPanel getInstance() {
		if (instance == null) {
			instance = new OrderHistoryPanel();
		}
		return instance;
	}
	        
	    
	public void actionPerformed(ActionEvent e) {
	       
	}
	
	/**
	 * Return the current table
	 * 
	 * @return table - JTable
	 */
	public JTable getTable() {
		return table;
	}
	
	/**
	 * Create the table
	 * 
	 * Return void
	 */
	public void createTable(){
		try {
			table = new JTable(new DefaultTableModel(getInfo(), GUIConstants.getOrderHistoryColumnNames())){
				private static final long serialVersionUID = 1L;
				//Make the table's cell uneditable.
				public boolean isCellEditable(int row, int col){		
					return false;
				}
			};
			
			table.setBounds(0, 0, 512, 334);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Refreshes the table to get updated info.
	 */
	public void refreshTable() {
		if (table != null) {		
			DefaultTableModel model = (DefaultTableModel)table.getModel();
			try {
				model.setDataVector(getInfo(), GUIConstants.getOrderHistoryColumnNames());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			this.updateUI();
		}
	}
	
	/**
	 * Return a 2 dimensional array that holds the row data
	 * used to create the table
	 * 
	 * @return getInfo
	 * @throws IOException
	 */
	public String[][] getInfo() throws IOException{
		LinkedList<Order> orders = UserInformation.getInstance().getUser().getClosedOrders();
		String[][] info = new String[orders.size()][8];
		int buySell, orderType;
		Long openTime, closeTime;
		Float amount;
		
		//Generate proper date strings from a long 
		SimpleDateFormat dO = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
		SimpleDateFormat dC = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
		String openDate = new String();
		String closeDate = new String();
			
		int i = 0;
		for (Order order : orders) {
			openTime = order.getOpenTime();
			openDate = dO.format(new Date(openTime));
			closeTime = order.getCloseTime();
			closeDate = dC.format(new Date(closeTime));
			  
			amount = order.getAmount();
			buySell = order.getBType();
			orderType = order.getOType();
			
			info[i][0] = i + "";
			info[i][1] = openDate;
			info[i][2] = closeDate;
			info[i][3] = order.getRatio();
			info[i][4] = order.getRate() + "";
			info[i][5] = amount.toString();
			
			switch(buySell){
				case 1:
					info[i][6] = "B";
					break;
				case 2:
					info[i][6] = "S";
					break;
			}
				
			switch(orderType){
				case 1:
					info[i][7] = "N";
					break;
				case 2:
					info[i][7] = "TS";
					break;
				case 3:
					info[i][7] = "L";
					break;
			}
			
			i++;
		}  
		return info;
	}
}
